package com.jike.mahjong.controller;


import com.jike.mahjong.common.Constants;
import com.jike.mahjong.common.Page;
import com.jike.mahjong.common.util.HttpUtil;
import com.jike.mahjong.service.StatisticsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 游戏统计
 * Created by star on 2016/10/12.
 */
@Controller
public class GameController {

    @Autowired
    private StatisticsService statisticsService;


    private Logger log = Logger.getLogger(GameController.class);


    @RequestMapping(value = "/game/statistics", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String staticPage(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
        List<Map<String, Object>> allIncomeInfo=new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> allPayInfo=new ArrayList<Map<String, Object>>();
        allIncomeInfo=statisticsService.queryAllIncomDiamond();
        allPayInfo= statisticsService.queryPayDiamonAll();
        allIncomeInfo.addAll(allPayInfo);
        model.addAttribute("allIncomeInfo",allIncomeInfo);
//        model.addAttribute("allPayInfo",allPayInfo);
        return "views/gameresult/gameStatisc";
    }


    /**
     * 查询日志
     * @param request
     * @param response
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/game/statisticsByDay", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String  statisticsInfo(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {


        List<Map<String, Object>> allIncomeInfo=new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> allPayInfo=new ArrayList<Map<String, Object>>();
        allIncomeInfo=statisticsService.queryAllIncomDiamond();
        allPayInfo= statisticsService.queryPayDiamonAll();
        allIncomeInfo.addAll(allPayInfo);


        Map<String, Object> paramMap =  HttpUtil.paramsToMap(request.getParameterMap());
        List<Map<String, Object>> dayInfo=new ArrayList<Map<String, Object>>();
        String queryType= (String) paramMap.get("queryType");

        List<Map<String, Object>> showDayDiamond=new ArrayList<Map<String, Object>>();

        if("0".equals(queryType)){
          dayInfo=statisticsService.queryPayDiamonByDay(paramMap);
            showDayDiamond=statisticsService.queryShowPayDiamond(paramMap);
        }else{
            dayInfo=statisticsService.queryIncomDiamondByDay(paramMap);
            showDayDiamond=statisticsService.queryShowIncomeDiamond(paramMap);
        }

        model.addAttribute("allIncomeInfo",allIncomeInfo);
        model.addAttribute("showDayDiamond",showDayDiamond);
        model.addAttribute("eachDayInfo",dayInfo);
        model.addAttribute("paramMap",paramMap);

        return "views/gameresult/gameStatisc";
    }



    /**
     * 查消费记录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/game/queryPayRecord.html", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryRechargeRecord(HttpServletRequest request, HttpServletResponse response,  Model model) {
        Map<String, Object> paramMap = HttpUtil.paramsToMap(request.getParameterMap());
        Page page=new Page();
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");

        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }

        model.addAttribute("page",statisticsService.queryPayRecord(paramMap,page));
        model.addAttribute("paramMap",paramMap);
        return "views/player/playerPayRecord";
    }


    /**
     * 管理员查询订单
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value = "/order/queryOrder.html", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryOrderByAgentId(HttpServletRequest request, HttpServletResponse response,  Model model) {
        Map<String, Object> paramMap = HttpUtil.paramsToMap(request.getParameterMap());
        Page page=new Page();
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");
//        paramMap.put("sourceType",HttpUtil.getHttpParameter(request, "sourceType"));
        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }

        model.addAttribute("page",statisticsService.queryRechargeInfoForAgent(paramMap,page));
        model.addAttribute("paramMap",paramMap);
        return "views/gameresult/orderStatisc";
    }

    /**
     * 代理商的订单充值查询
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value = "/agent/order/queryOrder.html", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object queryOrderForAgent(HttpServletRequest request, HttpServletResponse response,  Model model) {
        Map<String, Object> paramMap=new HashMap<String, Object>();
        Page page=new Page();
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");
        String agentId = request.getSession().getAttribute("s_user_id").toString();

        paramMap.put("playerIdSearch",HttpUtil.getHttpParameter(request, "playerIdSearch"));
        paramMap.put("begintime",HttpUtil.getHttpParameter(request, "begintime"));
        paramMap.put("endtime",HttpUtil.getHttpParameter(request, "endtime"));
        paramMap.put("sourceType",HttpUtil.getHttpParameter(request, "sourceType"));
        paramMap.put("agentId",agentId);
        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }
        paramMap.put("errorCode", "0");
        paramMap.put("page",statisticsService.queryRechargeInfoForAgent(paramMap,page));
        paramMap.putAll(statisticsService.queryStatisticsInfo(paramMap));
        return paramMap;
    }




}
